package com.wanmait.community.controller;

import com.wanmait.community.aop.OperationLogInfo;
import com.wanmait.community.entity.Admin;
import com.wanmait.community.entity.AdminRole;
import com.wanmait.community.entity.Role;
import com.wanmait.community.service.AdminRoleService;
import com.wanmait.community.service.AdminService;
import com.wanmait.community.service.RoleService;
import com.wanmait.community.util.AjaxResult;
import com.wanmait.community.util.IPUtils;
import com.wanmait.community.util.JWTUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private AdminRoleService adminRoleService;
    @Resource
    private RoleService roleService;
    @OperationLogInfo("查询物业的所有员工")
    @GetMapping("list")
    public AjaxResult list() {
        List<Admin> adminList = adminService.findAll();
        return AjaxResult.success().put("adminList", adminList);
    }
    @GetMapping("selectAdminByPhone/{phone}")
    public AjaxResult selectAdminByPhone(@PathVariable String phone) {
        Admin admin = adminService.selectAdminByPhone(phone);
        return AjaxResult.success().put("admin", admin);
    }
    @GetMapping("selectAdminByInNum/{inNum}")
    public AjaxResult selectAdminByInNum(@PathVariable String inNum) {
        Admin admin = adminService.selectAdminByInNum(inNum);
        return AjaxResult.success().put("admin", admin);
    }
    @PostMapping("login")
    public AjaxResult login(@RequestBody Admin admin, HttpServletRequest response) {
        String ip = IPUtils.getClientIpAddr(response);
        admin.setLoginIp(ip);
        return adminService.login(admin);
    }
    @GetMapping("adminRole")
    public AjaxResult adminRole() {
        List<Role> roleList = roleService.list();
        return AjaxResult.success().put("roleList", roleList);
    }
    @OperationLogInfo("修改一个物业的员工")
    @PostMapping("update")
    public AjaxResult update(@RequestBody Admin admin,Integer roleId,HttpServletRequest request) {
        AdminRole adminRole = new AdminRole();
        adminRole.setAdminId(admin.getId());
        adminRole.setRoleId(roleId);
        String token = request.getHeader("token");
        Integer adminId = Integer.parseInt(JWTUtils.getAudience(token));
        admin.setUpdateBy(adminId);
        adminService.updateById(admin);
        adminRoleService.updateAdminRoleByAdmin(adminRole);
        return AjaxResult.success("操作成功");
    }
    @OperationLogInfo("添加物业的员工")
    @PostMapping("insert")
    public AjaxResult insert(@RequestBody Admin admin,Integer roleId,HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer adminId = Integer.parseInt(JWTUtils.getAudience(token));
        admin.setUpdateBy(adminId).setCreatBy(adminId);
        adminService.insertAdmin(admin,roleId);
        return AjaxResult.success("操作成功");
    }
    @OperationLogInfo("开除一个物业的员工")
    @DeleteMapping("delete")
    public AjaxResult delete(Integer id) {
        adminService.deleteAdminById(id);
        return AjaxResult.success("修改成功");
    }
    @PostMapping("uploadPic")
    public AjaxResult uploadPic(MultipartFile file) {
        try {
            if(!file.isEmpty()) {
                //文件保存的物理路径
                String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadImages/";
                //生成一个唯一的文件名
                String fileExt = FilenameUtils.getExtension(file.getOriginalFilename());
                String fileName = UUID.randomUUID().toString().replace("-","")+"."+fileExt;
                file.transferTo(new File(path,fileName));
                return AjaxResult.success("图片上传成功").put("fileName", fileName);
            }
            else {
                return AjaxResult.error("图片不能为空");
            }
        } catch (FileNotFoundException e) {
            return AjaxResult.error("不存在的文件路径");
        } catch (IOException e) {
            return AjaxResult.error("图片上传失败");
        }
    }
}
